// door.txt - This is the basic script that powers all doors. If you are 
// working on a scenario that contains doors, DO NOT REMOVE THIS SCRIPT.

// If you place a door, this script is automatically attached to it. The
// default settings for the script are for a basic, unlocked door. To make it
// locked, you will need to change the memory cells.

// Memory Cells - 
//   0 - Lock level. If left at 0, door is unlocked. Otherwise, the strength/tool
//     use needed to get it open. If this is set to a really high number (say, 200),
//     it can't be unlocked by normal means
//   1 - Key needed. If left at 0, no special item helps unlock the door. Otherwise,
//     if that party has this special item, the door automatically unlocks.
//   2,3 - Coordinates for a stuff done flag. If these are 0 and 0, ignored. Otherwise,
//     the stuff done flag is set to 1 when the door is unlocked. If the flag is non-zero,
//     than when the party enters this zone, the door will ebcome unlocked.
//   4 - Has magic? If it does, you need to cast unlock doors before picking the lock.
//   5 - Has trap? If it does, runs town script at state this number. No extra SDF has to be attached.

beginterrainscript; 

variables;
	short i_am_open = 0;
	short cur_terrain;
	short i_am_locked = 0;
	
	short door_opened;
	short choice;
	int open_snd;
	int closed_snd;
	int x, y;
	int magic_gone = 0;
	int trap_gone = 0;
body;

beginstate INIT_STATE;
    x = my_loc_x();
    y = my_loc_y();
	cur_terrain = terrain_in_this_spot();
	if (((cur_terrain >= 14) && (cur_terrain <= 17)) ||
	  ((cur_terrain >= 50) && (cur_terrain <= 53) || (cur_terrain == 174) || (cur_terrain == 477)))
	  	i_am_open = 1;
	  	else i_am_open = 0;

	if (get_memory_cell(0) > 0) {
		i_am_locked = 1;
		set_mechanism_difficulty(get_memory_cell(0));
		set_physical_strength(get_memory_cell(0));
		
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
			if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
				i_am_locked = 0;
				magic_gone = 1;
				trap_gone = 1;
			}
		}
	if ((floor_in_this_spot() == 130) || (get_floor(x - 1, y - 1) == 130) ||
	     (get_floor(x, y - 1) == 130) || (get_floor(x + 1, y - 1) == 130) || 
	     (get_floor(x - 1, y) == 130) || (get_floor(x + 1, y - 1) == 130) || 
	     (get_floor(x - 1, y + 1) == 130) || (get_floor(x, y + 1) == 130) || 
	     (get_floor(x + 1, y + 1) == 130)) {
	    open_snd = -110;
	    closed_snd = -129;
	    }
	else {
	    open_snd = -159;
	    closed_snd = -59;
	    }
	if ((terrain_in_this_spot() == 169) || (terrain_in_this_spot() == 174) || (terrain_in_this_spot() == 476) || (terrain_in_this_spot() == 477))
	    open_snd = -58;
	break;

beginstate START_STATE;
break;

beginstate SEARCH_STATE;
	if (i_am_open == 1) {
		print_str_color("You close the door.",2);
		flip_terrain(my_loc_x(),my_loc_y());
		i_am_open = 0;
		play_sound(closed_snd);
		}
break;

beginstate BLOCK_MOVE_STATE;
	if (i_am_open == 0) {
		block_entry(1);
		
		door_opened = 1;

		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
			if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
				i_am_locked = 0;
			}
		
		if ((get_memory_cell(1) > 0) && (i_am_locked > 0)) {
			if (has_special_item(get_memory_cell(1))) {
				print_str_color("You have the key which unlocks this door.",2);
				if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
					set_flag(get_memory_cell(2),get_memory_cell(3),1);
				i_am_locked = 0;
				}
			}
		if ((get_memory_cell(4) != 0) && (i_am_locked) && (magic_gone == 0)) {
		    message_dialog("This container is locked. You try to examine the padlock, but it is surrounded by a faint field of energy. You can't reach it until you remove the enchantment with a spell of your own.", "");
		    end();
		    }		    
		if (i_am_locked) {
		    if (closed_snd == -129)
		        set_state_continue(3);
                        play_sound(106);
			reset_dialog();
			if (get_memory_cell(1) > 0)
				add_dialog_str(0,"This door is locked, and you don't have the right key. You can have your strongest character try to bash it down (which requires high strength) or have your most skilled character try to pick the lock (which requires Tool Use skill and a lockpick).",0);
				else add_dialog_str(0,"This door is locked. You can have your strongest character try to bash it down (which requires high strength) or have your most skilled character try to pick the lock (which requires Tool Use skill and a lockpick).",0);
			add_dialog_choice(0,"Leave the door alone.");
			add_dialog_choice(1,"Try to bash it down.");
			add_dialog_choice(2,"Try to pick the lock.");
			choice = run_dialog(0);
			
			if (choice == 1)
				end();
			if (choice == 2) {
				if (run_bash_door(get_physical_strength()) == FALSE)
					door_opened = 0;
				}
			if (choice == 3) {
				if (run_pick_lock(get_mechanism_difficulty()) == FALSE) 
					door_opened = 0;
					else if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
						award_party_xp(BASE_TRAP_XP,1 + 2 * get_mechanism_difficulty());
					
				}
			}
	    
		if (get_memory_cell(5) != 0) {
		    if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
			    if (get_flag(get_memory_cell(2),get_memory_cell(3)) != 0) {
		        run_town_script(get_memory_cell(5));
		        if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
					set_flag(get_memory_cell(2),get_memory_cell(3),1);
					}
					}
		if (door_opened) {
			print_str("You open the door.");
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
				set_flag(get_memory_cell(2),get_memory_cell(3),1);
			flip_terrain(my_loc_x(),my_loc_y());
			i_am_open = 1;
			i_am_locked = 0;
			play_sound(open_snd);
			}
		}
break;

beginstate UNLOCK_SPELL_STATE;
    if ((get_memory_cell(4) != 0) && (magic_gone == 0) && (get_unlock_spell_strength() >= get_memory_cell(4))) {
        print_str_color("The magical part of the lock has been removed", 2);
        print_str_color("  (It can now be picked normally.)", 2);
        play_sound(89);
        magic_gone = 1;
        end();
        }
    else if ((get_memory_cell(4) != 0) && (magic_gone == 0) && (get_unlock_spell_strength() < get_memory_cell(4))) {
        print_str_color("Unlock Doors: The spell isn't strong enough to affect the door.",2);
        end();
        }
	    if ((i_am_open > 0) || (i_am_locked == 0))
		print_str_color("Unlock Doors: The spell doesn't affect unlocked doors.",2);
		else {
			if (get_unlock_spell_strength() >= get_mechanism_difficulty() * 2) {
				print_str_color("Unlock Doors: The spell unlocks a door.",2);
				i_am_locked = 0;
				play_sound(9);
				if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
					set_flag(get_memory_cell(2),get_memory_cell(3),1);
				}
				}
				
				else {
					print_str_color("Unlock Doors: The spell isn't strong enough to affect the door.",2);
					if (get_memory_cell(4) == 0)
					print_str_color("  (This spell usually only affects doors with magical protection.)",2);
					}
			}
break;

beginstate DISPEL_BARRIER_STATE;
	if ((i_am_open == 0) && (i_am_locked)) {
		print_str_color("Dispel Barrier: The spell fails to affect a locked door.",2);
		}
break;
beginstate 3; // Created a whole different lock mechanism for vahnatai doors.
    if (has_item_of_class(200, 0) == 0) {
        if (get_memory_cell(1) != 0)
            message_dialog("This door is locked with a lock which is clear vahnatai construction.", "You can't open it, because you haven't got the right key, nor have you any suitable lockpicks.");
        else message_dialog("This door is locked with a lock which is clear vahnatai construction.", "You can't open it, because you haven't got any suitable lockpicks.");
        }
    else {
        reset_dialog();
        add_dialog_str(0, "This door is locked. It is made from a crystal type which looks far too hard to be bashed down. You may still attempt to pick the lock (which requires Tool Use skill and a suitable lockpick).", 0);
        add_dialog_choice(0, "Leave the door alone.");
        add_dialog_choice(1,"Try to pick the lock.");
        choice = run_dialog(0);
        if (choice == 2) {
        if (has_item(454)) {
            play_sound(-9);
            print_str_color("Pick Lock: Your magical vahnatai lockpick has opened the lock.", 2);
            i_am_open = 1;
            i_am_locked = 0;
            }
        else {
            if (get_stat(char_with_highest_skill(15), 15) >= get_memory_cell(0)) {
                play_sound(-9);
                print_str_color("Pick Lock: You have managed to open the lock.", 2);
                i_am_open = 1;
                i_am_locked = 0;
                award_party_xp(BASE_TRAP_XP,1 + 3 * get_mechanism_difficulty());
                }
                }
                }
                }
 break;               
                